package com.zzjr.tools.java.ssm.factory;

import com.zzjr.tools.java.ssm.constants.AutoCodeConstants;
import com.zzjr.tools.java.ssm.utils.CamelUtils;
import com.zzjr.tools.java.ssm.utils.IOUtils;
import com.zzjr.tools.java.ssm.utils.StringUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Description: 用于生成Facade接口的工厂类
 * 
 * @Date Create on 2015年10月13日
 * @author <a href="mailto:Aromarich@zuozh.com">Aromarich</a>
 * @since version1.0 Copyright 2015 ZZJR All Rights Reserved.
 */
public class FacadeFactory {
	
	// Java Facade接口文件路径
	static private File facadeFile;
	
	// java Model对象名
	static private String modelName;
	
	// 日期格式转换工具类
	static private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	/**
	 * 创建Facade接口文件
	 */
	public static void generateFacade(String tbName) {
		System.out.println("开始生成Facade接口类");
		// tableName = tbName;
		modelName = StringUtils.upperFirstChar(CamelUtils.convertUnderLineToCamel(tbName));
		
		// 1.创建facade包
		String facadeDir = AutoCodeConstants.PACKAGE_FULL_PATH + File.separator + "biz" + File.separator
				+ AutoCodeConstants.FACADE_SUFFIX.toLowerCase();
		IOUtils.mkDir(new File(facadeDir));
		
		// 2.创建文件
		String filePath = facadeDir + File.separator + modelName + AutoCodeConstants.FACADE_SUFFIX + ".java";
		facadeFile = new File(filePath);
		IOUtils.createNewFile(facadeFile);
		
		// 3.往Java文件中写入内容
		writeToFacadeFile();
		System.out.println("完成Facade接口类的生成");
	}
	
	/**
	 * 创建Facade接口文件
	 */
	private static void writeToFacadeFile() {
		// 2.生成Facade接口主体代码
		StringBuilder content = parseFacadeFile();
		FileWriter fw;
		PrintWriter pw;
		try {
			fw = new FileWriter(facadeFile);
			pw = new PrintWriter(fw);
			pw.println(content.toString());
			pw.flush();
			pw.close();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 生成Facade接口文件的主方法
	 * 
	 * @return
	 */
	private static StringBuilder parseFacadeFile() {
		StringBuilder sb = new StringBuilder();
		
		sb.append("package " + AutoCodeConstants.PACKAGE_NAME + ".facade."
				+ AutoCodeConstants.FACADE_SUFFIX.toLowerCase() + ";\r\n\r\n");

		sb.append("import java.util.List;\r\n\r\n");
		sb.append("import com.zzjr.commons.remote.FacadeResult;\r\n\r\n");
		sb.append("import " + AutoCodeConstants.PACKAGE_NAME + ".dataobject." + modelName + "DTO;\r\n\r\n");
		
		sb.append("/**\r\n");
		sb.append(" * Description:" + modelName + " Facade接口\r\n");
		sb.append(" *\r\n");
		sb.append(" * @Date Create on " + sdf.format(new Date()) + "\r\n");
		sb.append(" * @author <a href=\"mailto:" + AutoCodeConstants.AUTHOR_NAME + "@zuozh.com\">"
				+ AutoCodeConstants.AUTHOR_NAME + "</a>\r\n");
		sb.append(" * @since version1.0 Copyright 2018 ZZJR All Rights Reserved.\r\n");
		sb.append(" */\r\n");
		
		sb.append("public interface " + modelName + AutoCodeConstants.FACADE_SUFFIX + " {\r\n\r\n");
		
		// 处理方法
		processMethod(sb);
		
		sb.append("}\r\n");
		
		return sb;
	}
	
	/**
	 * 生成接口方法
	 */
	private static void processMethod(StringBuilder sb) {
		// 1.insertXxx
		sb.append("\t/**\r\n");
		sb.append("\t * 将数据插入到数据库\r\n");
		sb.append("\t *\r\n");
		sb.append("\t * @param " + modelName + "\r\n");
		sb.append("\t * @return 返回成功新增的个数\r\n");
		sb.append("\t */\r\n");
		sb.append("\tint insert" + modelName + "(" + modelName + " " + CamelUtils.convertUnderLineToCamel(modelName)
				+ ") throws Exception;\r\n\r\n");
		
		// 2.deleteXxxById
		sb.append("\t/**\r\n");
		sb.append("\t * 根据ID删除数据\r\n");
		sb.append("\t *\r\n");
		sb.append("\t * @param id\r\n");
		sb.append("\t * @return 返回成功删除记录的个数\r\n");
		sb.append("\t */\r\n");
		sb.append("\tint delete" + modelName + "ById(Long id) throws Exception;\r\n\r\n");
		
		// 3.updateXxxById
		sb.append("\t/**\r\n");
		sb.append("\t * 根据ID修改数据\r\n");
		sb.append("\t *\r\n");
		sb.append("\t * @param " + CamelUtils.convertUnderLineToCamel(modelName) + "\r\n");
		sb.append("\t * @return 返回成功修改记录的个数\r\n");
		sb.append("\t */\r\n");
		sb.append("\tint update" + modelName + "ById(" + modelName + " "
				+ CamelUtils.convertUnderLineToCamel(modelName) + ") throws Exception;\r\n\r\n");
		
		// 4.selectXxxById
		sb.append("\t/**\r\n");
		sb.append("\t * 根据ID查询数据\r\n");
		sb.append("\t *\r\n");
		sb.append("\t * @param id\r\n");
		sb.append("\t * @return 返回成功查询记录\r\n");
		sb.append("\t */\r\n");
		sb.append("\t" + modelName + " select" + modelName + "ById(Long id) throws Exception;\r\n\r\n");

	}
}
